package net.alche.al.util.far.pojo;

import java.util.Map;

import net.alche.fa.core.SageStone;
import net.alche.fa.core.model.FaValue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

// ResultPack
@SuppressWarnings("all")
public class ResultPack {
  public int state = 1; // 响应状态 1=数据正常 0=其他异常 其他=异常代码
  public String msg; // 返回说明
  public Object data; // 主数据对象
  public Map mp; // 其他对象 在查询了主要数据,还需要查询相关的其它数据的情况先使用
  public PageList pageList; // 分页查询时 返回的对象

  public ResultPack() {
  }

  public ResultPack(PageList pageList) {
    this.pageList = pageList;
    if (pageList.getFavList() != null) {
      data = pageList.getFavList().getJava();
      pageList.setFavList(null);
    }
  }

  public ResultPack(Object data) {
    this.setData(data);
  }

  // public ResultPack(ArrayList ls) {
  // if (ls != null && !ls.isEmpty()) {
  // Object obj = ls.get(0);
  // if (obj instanceof SageStone || obj instanceof FaValue) {
  // List newLs = new ArrayList<>();
  // // boolean isFav = obj instanceof FaValue;
  // for (Object it : ls) {
  // // SageStone st = (SageStone) it;
  // FaValue fav = new FaValue(it);
  // newLs.add(fav.getJava());
  // }
  // this.setData(newLs);
  // return;
  // }
  // }
  // this.setData(ls);
  // }

  // public ResultPack(SageStone data) {
  // FaValue fav = new FaValue(data);
  // this.setData(fav.getJava());
  // }

  public ResultPack setErr(String msg) {
    this.msg = msg;
    this.state = 0;
    return this;
  }

  public int getState() {
    return state;
  }

  public void setState(int state) {
    this.state = state;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public Object getData() {
    return data;
  }

  public void setData(Object data) {
    // if ( data != null && data instanceof IPage) {
    // this.pageList = AC.mybatis.toPage((IPage) data);
    // }
    // else {
    this.data = data;
    // }
  }

  public ResultPack(SageStone data) {
    FaValue fav = new FaValue(data);
    this.setData(fav.getJava());
  }

  public PageList getPageList() {
    return pageList;
  }

  public void setPageList(PageList pageList) {
    this.pageList = pageList;
  }

  public Map<String, Object> getMap() {
    return mp;
  }

  // 添加其他数据
  // @param objName 对象名称
  // @param obj 对象
  public void addObj(String objName, Object obj) {
    if (mp == null) {
      mp = new HashMap<>();
    }
    mp.put(objName, obj);
  }

  public ResultPack addError(String errMsg) {
    state = 0;
    if (msg != null) {
      msg += ", " + errMsg;
    } else {
      msg = errMsg;
    }
    return this;
  }

  public ResultPack success(String txt) {
    state = 1;
    data = txt;
    return this;
  }
}